package com.atguign.gulimall.product.service.impl;

import com.atguign.gulimall.product.dao.BrandDao;
import com.atguign.gulimall.product.dao.CategoryDao;
import com.atguign.gulimall.product.entity.BrandEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguign.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguign.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguign.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setBrandName(brandDao.selectById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryDao.selectById(catelogId).getName());
        this.baseMapper.insert(categoryBrandRelation);
    }

    @Transactional
    @Override
    public void updateBrand(Long brandId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId)
                .set(CategoryBrandRelationEntity::getBrandName, name);
        this.update(queryWrapper);
    }

    @Transactional
    @Override
    public void updateCascade(Long catId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId)
                .set(CategoryBrandRelationEntity::getCatelogName, name);
        this.update(queryWrapper);
    }

    @Override
    public List<BrandEntity> getBrabdsBuCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(
                new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                        .eq(CategoryBrandRelationEntity::getCatelogId, catId)
        );
        List<Long> collect = categoryBrandRelationEntities
                .stream()
                .map(CategoryBrandRelationEntity::getBrandId)
                .collect(Collectors.toList());
        return brandDao.selectBatchIds(collect);
    }
}